using System;
using System.Collections.Generic;
using System.Windows.Forms;
using BLL;
using Model;

namespace UI {
    public partial class FrmCheckout : Form {
        private DeskBLL _deskBLL = DeskBLL.Instance;
        private RADwithOrderId _radwithOrderId;
        private OrderProductBLL _orderProductBll = new OrderProductBLL();
        private OrderBLL _orderBll = OrderBLL.Instance;
        private MemberBLL _memberBll = MemberBLL.GetMemberBll();
        private Member _member;
        public FrmCheckout() {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
        }

        public void Init(RADwithOrderId raDwithOrderId) {
            _radwithOrderId = raDwithOrderId;
            label2.Text = raDwithOrderId.OrderId.ToString();
            label3.Text = raDwithOrderId.Desk.DeskName;
            List<OrderProductDTO> dtos = _orderProductBll.QueryAllByOrderId(_radwithOrderId.OrderId);
            dgvROrderProduct.DataSource = dtos;
            decimal money = 0;
            foreach (OrderProductDTO orderProductDto in dtos) {
                money += orderProductDto.totalPrice;
            }
            label5.Text = money.ToString();
            label13.Text = money.ToString();
            label14.Text = money.ToString();
            label16.Text = "";
            label8.Text = "";
            label10.Text = "";
        }

        private void textBox1_TextChanged(object sender, EventArgs e) {
            _member = _memberBll.QueryByPhone(textBox1.Text);
            if (_member != null) {
                label16.Text = _member.Mname;
                label8.Text = _member.Money.ToString();
                label10.Text = _member.Discount.ToString();
                label14.Text = (_member.Discount * 0.1 * double.Parse(label5.Text)).ToString();
            } else {
                label14.Text = label5.Text;
                label16.Text = "";
                label8.Text = "";
                label10.Text = "";
            }
        }

        //结账
        private void button1_Click(object sender, EventArgs e) {
            //判断房间低消
            if (_radwithOrderId.Room.RoomMinConsumption > int.Parse(label5.Text)) {
                MessageBox.Show("低消都够不着,穷人别来");
                return;
            }
            double discount = 0;
            //是否会员
            if (_member == null) {
                //直接支付
                MessageBox.Show("pay " + label13.Text);
            } else {
                discount = _member.Discount;
                //是会员,看卡内余额是否满足
                if (int.Parse(label8.Text) < int.Parse(label14.Text)) {
                    MessageBox.Show("余额不足~");
                    return;
                }
                //更新会员状态,增加积分,扣减余额
                _member.Money -= int.Parse(label14.Text);
                _member.Points += int.Parse(label14.Text);
                _memberBll.UpdateMember(_member);
            }
            //完善订单表
            _orderBll.Update(new Order() {
                OrderId = _radwithOrderId.OrderId,
                EndTime = DateTime.Now,
                OrderMoney = int.Parse(label14.Text),
                Discount = decimal.Parse(discount.ToString()),
                OrderState = 1
            });
            //更新订单产品表
            _orderProductBll.Update(_radwithOrderId.OrderId);
            //更新桌子状态
            _radwithOrderId.Desk.DeskState = 0;
            _deskBLL.UpdateDeskState(_radwithOrderId.Desk);
            MessageBox.Show("Checkout is success");
        }
    }
}